/**
 * Form Validation Utilities
 * Provides validation functions for authentication forms
 */

export const ValidationUtils = {
  /**
   * Validate username
   * @param {string} username - Username to validate
   * @returns {Object} Validation result with isValid and error message
   */
  validateUsername(username) {
    if (!username || username.trim() === '') {
      return {
        isValid: false,
        error: '用户名不能为空'
      };
    }
    
    if (username.length < 3) {
      return {
        isValid: false,
        error: '用户名至少需要3个字符'
      };
    }
    
    return {
      isValid: true,
      error: null
    };
  },

  /**
   * Validate password
   * @param {string} password - Password to validate
   * @returns {Object} Validation result with isValid and error message
   */
  validatePassword(password) {
    if (!password || password.trim() === '') {
      return {
        isValid: false,
        error: '密码不能为空'
      };
    }
    
    if (password.length < 6) {
      return {
        isValid: false,
        error: '密码至少需要6个字符'
      };
    }
    
    return {
      isValid: true,
      error: null
    };
  },

  /**
   * Validate phone number
   * @param {string} phone - Phone number to validate
   * @returns {Object} Validation result with isValid and error message
   */
  validatePhone(phone) {
    if (!phone || phone.trim() === '') {
      return {
        isValid: false,
        error: '手机号不能为空'
      };
    }
    
    // Chinese mobile phone number pattern (11 digits starting with 1)
    const phonePattern = /^1[3-9]\d{9}$/;
    
    if (!phonePattern.test(phone)) {
      return {
        isValid: false,
        error: '请输入有效的手机号码'
      };
    }
    
    return {
      isValid: true,
      error: null
    };
  },

  /**
   * Validate login form
   * @param {Object} formData - Form data with username and password
   * @returns {Object} Validation result with isValid and errors object
   */
  validateLoginForm(formData) {
    const errors = {};
    let isValid = true;

    const usernameValidation = this.validateUsername(formData.username);
    if (!usernameValidation.isValid) {
      errors.username = usernameValidation.error;
      isValid = false;
    }

    const passwordValidation = this.validatePassword(formData.password);
    if (!passwordValidation.isValid) {
      errors.password = passwordValidation.error;
      isValid = false;
    }

    return {
      isValid,
      errors
    };
  },

  /**
   * Validate registration form
   * @param {Object} formData - Form data with username, password, and phone
   * @returns {Object} Validation result with isValid and errors object
   */
  validateRegistrationForm(formData) {
    const errors = {};
    let isValid = true;

    const usernameValidation = this.validateUsername(formData.username);
    if (!usernameValidation.isValid) {
      errors.username = usernameValidation.error;
      isValid = false;
    }

    const passwordValidation = this.validatePassword(formData.password);
    if (!passwordValidation.isValid) {
      errors.password = passwordValidation.error;
      isValid = false;
    }

    const phoneValidation = this.validatePhone(formData.phone);
    if (!phoneValidation.isValid) {
      errors.phone = phoneValidation.error;
      isValid = false;
    }

    return {
      isValid,
      errors
    };
  },

  /**
   * Clear validation errors for a specific field
   * @param {Object} errors - Current errors object
   * @param {string} field - Field name to clear
   * @returns {Object} Updated errors object
   */
  clearFieldError(errors, field) {
    const updatedErrors = { ...errors };
    delete updatedErrors[field];
    return updatedErrors;
  },

  /**
   * Check if there are any validation errors
   * @param {Object} errors - Errors object
   * @returns {boolean} True if there are errors
   */
  hasErrors(errors) {
    return Object.keys(errors).length > 0;
  }
};